<?php

namespace app\admin\controller\addons;

use app\admin\controller\AdminController;
use app\admin\service\addons\ManageService;
use app\admin\validate\addons\ManageValidate;
use think\response\Json;
use tmcore\services\addons\AddonApiService;
use tmcore\services\addons\CoreService;
use tmcore\services\addons\TerminalService;

class Manage extends AdminController
{

    /**
     * 获取用户信息
     * @return Json
     */
    public function userInfo()
    {
        return $this->data(ManageService::userInfo());
    }

    /**
     * 用户登录
     * @return Json
     */
    public function login()
    {
        $params = (new ManageValidate())->checkParam('login');
        $result = ManageService::login($params);
        if ($result) {
            return $this->success([],'登录成功');
        } else {
            return $this->error(ManageService::getError());
        }
    }

    /**
     * 退出登录
     * @return Json
     */
    public function logout()
    {
        $result = ManageService::logout();
        if ($result) {
            return $this->success([],'退出成功');
        } else {
            return $this->error(ManageService::getError());
        }
    }

    /**
     * 刷新缓存
     * @return Json
     */
    public function refresh()
    {
        ManageService::refresh();
        return $this->success([],'刷新成功');
    }

    /**
     * 获取全部插件
     * @return Json
     */
    public function lists()
    {
        return $this->data(ManageService::getList($this->getData()));
    }

    /**
     * 获取分类列表
     * @return Json
     */
    public function categoryLists()
    {
        return $this->data(AddonApiService::get_addons_category_list($this->getData()));
    }

    /**
     * 获取本地插件列表
     * @return Json
     */
    public function localLists()
    {
        return $this->data(ManageService::getLocalList());
    }

    /**
     * 获取插件详情
     * @return Json
     */
    public function detail()
    {
        $params = (new ManageValidate())->checkParam('detail');
        return $this->data(ManageService::getDetail($params));
    }

    /**
     * 获取本地插件详情
     * @return Json
     */
    public function localDetail()
    {
        $params = (new ManageValidate())->checkParam('localDetail');
        return $this->data(ManageService::getLocalDetail($params));
    }

    /**
     * 插件下载
     * @return Json
     */
    public function download()
    {
        $params = (new ManageValidate())->post()->checkParam('download');
        $result = ManageService::downloadAddon($params);
        if (true === $result) {
            return $this->success([],'下载成功');
        } else {
            return $this->error(ManageService::getError());
        }
    }

    /**
     * 插件安装
     * @return Json
     */
    public function install()
    {
        $params = (new ManageValidate())->post()->checkParam('install');
        (new CoreService($params['identify']))->install($params);
        return $this->success();
    }

    /**
     * 插件卸载
     * @return Json
     */
    public function uninstall()
    {
        $params = (new ManageValidate())->post()->checkParam('uninstall');
        (new CoreService($params['identify']))->uninstall();
        return $this->success();
    }

    /**
     * 插件更新
     * @return Json
     */
    public function update()
    {
        $params = (new ManageValidate())->post()->checkParam('update');
        (new CoreService($params['identify']))->update($params);
        return $this->success();
    }

    /**
     * 插件导入
     * @return Json
     */
    public function import()
    {
        $params = (new ManageValidate())->post()->checkParam('import');
        $result = ManageService::addonImport($params);
        if ($result) {
            return $this->success();
        } else {
            return $this->error(ManageService::getError());
        }
    }

    /**
     * 获取依赖
     * @return Json
     * @throws \tmcore\exceptions\ValidateException
     */
    public function depends()
    {
        $params = (new ManageValidate())->checkParam('depends');
        return $this->data(ManageService::getDepends($params));
    }

    /**
     * 获取命令行
     * @return Json
     */
    public function commands()
    {
        $params = (new ManageValidate())->checkParam('commands');
        return $this->data(ManageService::getCommands($params));
    }

    /**
     * 执行命令
     * @return Json
     */
    public function execute()
    {
        $result = ManageService::getCommandExecInfo();
        if ($result) {
            return $this->success(['info' => $result]);
        } else {
            return $this->error(ManageService::getError());
        }
    }


    /**
     * 执行终端
     * @return void
     * @throws \Throwable
     */
    public function terminal()
    {
        (new TerminalService())->execute();
    }
}